package com.mv.webserver.core;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class WebServer  implements Runnable{
    private ServerSocket server;
    private ExecutorService threadPool;
    
  
	public WebServer(int port) {
        try {
            System.out.println("正在启动服务端...");
            server = new ServerSocket(port);
            threadPool = Executors.newFixedThreadPool(50);
            System.out.println("启动服务端完毕!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
	
	@Override
	public void run() {
		// TODO Auto-generated method stub
		
	}
	public void start() {
		
		Runnable serviceRunnable = new Runnable(){
			public void run(){
			     System.out.println("Runnable running");
			     while (true) {
			            try {
			                System.out.println("等待客户端连接...");
			                Socket socket = server.accept();
			                ClientHandler handler = new ClientHandler(socket);
			                threadPool.execute(handler);
			                System.out.println("一个客户端连接了!");
			            } catch (IOException e) {
			                e.printStackTrace();
			            }
			        }			     
			   }
		};
		Thread thread = new Thread(serviceRunnable);
		thread.start();
    }
        
 
    public static void main(String[] args) {
        WebServer server = new WebServer(8080);
        server.start();
        while(true) {
        	 System.out.println("主线程"); 
             try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}  
        }
    }



}